<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- <h2>组织架构内容</h2> -->
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <TreeTools :treeNode="company" :isRoot="true" @addDepts="addDepts" />
        <!-- 放置一个el-tree -->
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <TreeTools
            slot-scope="{ data }"
            :treeNode="data"
            @delDepts="getDepartments"
            @addDepts="addDepts"
            @editDepts="editDepts"
          />
          <!-- 传入内容, 插槽内容 , 作用域插槽, slot-scope = 'Obj' -->
        </el-tree>
      </el-card>
    </div>
    <!-- 放置新增弹层组件 -->
    <AddDept
      ref="addDept"
      :showDialog.sync="showDialog"
      :treeNode="node"
      @addDepts="getDepartments"
    />
  </div>
</template>

<script>
import TreeTools from "./components/tree-tools.vue";
import { getDepartments } from "@/api/departments";
import { tranListToTreeData } from "@/utils";
import AddDept from "./components/add-dept.vue";
export default {
  components: {
    TreeTools,
    AddDept,
  },
  data() {
    return {
      departs: [],
      defaultProps: {
        label: "name",
        children: "children",
      },
      company: {},
      showDialog: false, // 默认不显示
      node: null, // 记录当前节点 --- 添加, 编辑
    };
  },
  created() {
    this.getDepartments();
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments();
      this.company = {
        name: result.companyName,
        manger: "负责人",
        id: "",
      };
      this.departs = tranListToTreeData(result.depts, "");
      console.log(result, "result");
    },
    // 监听tree-tools中触发的点击添加子部门的事件
    addDepts(node) {
      this.showDialog = true; // 显示弹层
      this.node = node;
      console.log(node, "node");
    },
    editDepts(node) {
      console.log("edit");
      this.showDialog = true;
      this.node = node;
      this.$refs.addDept.getDepartDetail(node.id);
    },
  },
};
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>